﻿package mortal.game.view.shop
{
    import Message.DB.Tables.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.game.view.shopMall.view.*;
    import mortal.mvc.core.*;

    public class ShopCellRenderer extends GCellRenderer
    {
        private var _npcId:int;
        private var _shopItem:ShopSellItem;
        private var _itemName:GTextFiled;
        private var _price:GTextFiled;
        private var _moneyIcon:GBitmap;
        private var _tshop:TShop;

        public function ShopCellRenderer()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.doubleClickEnabled = true;
            this.configEventListener(MouseEvent.DOUBLE_CLICK, this.doubClickHandler);
            this.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this.pushUIToDisposeVec(UIFactory.bg(0, 0, 145, 50, this, ImagesConst.InputDisablBg));
            this._shopItem = UICompomentPool.getUICompoment(ShopSellItem);
            this._shopItem.x = 5;
            this._shopItem.y = 4;
            this._shopItem.setItemStyle(ItemStyleConst.Small, ImagesConst.PackItemBg, 2, 2);
            this.addChild(this._shopItem);
            this._shopItem.mouseChildren = false;
            this._shopItem.doubleClickEnabled = true;
            this._shopItem.configEventListener(MouseEvent.DOUBLE_CLICK, this.doubClickHandler);
            this._shopItem.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._itemName = UIFactory.gTextField("", 55, 5, 120, 20, this, null, true);
            this._itemName.mouseEnabled = false;
            this._moneyIcon = UIFactory.gBitmap("", 55, 23, this);
            var _loc_1:* = GlobalStyle.textFormatPutong;
            _loc_1.align = AlignMode.LEFT;
            this._price = UIFactory.gTextField("", 75, 24, 60, 20, this, _loc_1);
            this._price.mouseEnabled = false;
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.setMoneyInfo);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._shopItem.dispose(param1);
            this._itemName.dispose(param1);
            this._moneyIcon.dispose(param1);
            this._price.dispose(param1);
            this._shopItem = null;
            this._itemName = null;
            this._moneyIcon = null;
            this._price = null;
            this._tshop = null;
            super.disposeImpl(param1);
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.setMoneyInfo);
            return;
        }// end function

        private function setMoneyIcon(param1:int) : void
        {
            this._moneyIcon.bitmapData = GlobalClass.getBitmapData(GameDefConfig.instance.getEPrictUnitImg(param1));
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            this._shopItem.shopItemData = param1.data as ShopItemData;
            this._npcId = param1.npcId as int;
            this._tshop = ShopConfig.instance.getTShopByCode(this._shopItem.shopItemData.tShopSell.shopCode);
            this.setMoneyIcon(this._tshop.unit);
            this._itemName.htmlText = ItemsUtil.getItemName(this._shopItem.itemData);
            if (this._shopItem.shopItemData.tShopSell.activeOffer != 0)
            {
                this._price.text = this._shopItem.shopItemData.tShopSell.activeOffer.toString();
            }
            else
            {
                this._price.text = this._shopItem.shopItemData.tShopSell.offer.toString();
            }
            this.setMoneyInfo();
            return;
        }// end function

        private function setMoneyInfo(param1:Object = null) : void
        {
            if (!Cache.instance.role.enoughMoney(this._tshop.unit, int(this._price.text), null, true))
            {
                this._price.setTextFormat(GlobalStyle.textFormatHong.left());
            }
            else
            {
                this._price.setTextFormat(GlobalStyle.textFormatPutong.left());
            }
            return;
        }// end function

        private function doubClickHandler(event:MouseEvent) : void
        {
            BuyItemWin.instance.showWin(this._shopItem.shopItemData, this._npcId);
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            if (CursorManager.currentCurSorType == CursorManager.BUY)
            {
                BuyItemWin.instance.showWin(this._shopItem.shopItemData, this._npcId);
            }
            else if (CursorManager.currentCurSorType == CursorManager.Arrow)
            {
            }
            return;
        }// end function

    }
}
